Public Class frmUtilityRegister
    Public mblnCalledInner As Boolean

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub frmUtilityRegister_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If gbIsLicensed = False And mblnCalledInner = False Then
            End
        End If
    End Sub

    Private Sub frmUtilityRegister_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sProdNo As String

        Try
            sProdNo = GetLocalKey()
            txtSN1.Text = Strings.Mid(sProdNo, 1, 4)
            txtSN2.Text = Strings.Mid(sProdNo, 5, 4)
            txtSN3.Text = Strings.Mid(sProdNo, 9, 4)
            txtSN4.Text = Strings.Mid(sProdNo, 13, 4)
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub btnHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHelp.Click
        Me.Height = 482
    End Sub

    Private Sub txtSN1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSN1.GotFocus
        txtSN1.SelectAll()
    End Sub

    Private Sub txtSN2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSN2.GotFocus
        txtSN2.SelectAll()
    End Sub

    Private Sub txtSN3_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSN3.GotFocus
        txtSN3.SelectAll()
    End Sub

    Private Sub txtSN4_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSN4.GotFocus
        txtSN4.SelectAll()
    End Sub

    Private Sub txtLicense1_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLicense1.EditValueChanged
        SetFocusTextBox(txtLicense1)
    End Sub

    Private Sub SetFocusTextBox(ByVal txtTemp As DevExpress.XtraEditors.TextEdit)
        If Strings.Len(txtTemp.Text) = 4 Then
            If txtTemp.Name = "txtLicense1" Then
                txtLicense2.Focus()
            ElseIf txtTemp.Name = "txtLicense2" Then
                txtLicense3.Focus()
            ElseIf txtTemp.Name = "txtLicense3" Then
                txtLicense4.Focus()
            ElseIf txtTemp.Name = "txtLicense4" Then
                txtLicensee.Focus()
            End If
        End If
    End Sub

    Private Sub txtLicense1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLicense1.GotFocus
        txtLicense1.SelectAll()
    End Sub

    Private Sub txtLicense2_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLicense2.EditValueChanged
        SetFocusTextBox(txtLicense2)
    End Sub

    Private Sub txtLicense2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLicense2.GotFocus
        txtLicense2.SelectAll()
    End Sub

    Private Sub txtLicense3_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLicense3.EditValueChanged
        SetFocusTextBox(txtLicense3)
    End Sub

    Private Sub txtLicense3_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLicense3.GotFocus
        txtLicense3.SelectAll()
    End Sub

    Private Sub txtLicense4_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLicense4.EditValueChanged
        SetFocusTextBox(txtLicense4)
    End Sub

    Private Sub txtLicense4_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLicense4.GotFocus
        txtLicense4.SelectAll()
    End Sub

    Private Sub txtLicensee_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLicensee.GotFocus
        txtLicensee.SelectAll()
    End Sub

    Private Sub btnClipboard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClipboard.Click
        My.Computer.Clipboard.Clear()
        My.Computer.Clipboard.SetText(txtSN1.Text & txtSN2.Text & txtSN3.Text & txtSN4.Text)

        MessageBox.Show("Serial number copied to clipboard", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub txtLicense1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLicense1.TextChanged
        Dim strLicense As String
        Try
            strLicense = My.Computer.Clipboard.GetText
            If txtLicense1.Text.Length > 4 Then
                txtLicense1.Text = Strings.Mid(strLicense, 1, 4)
                txtLicense2.Text = Strings.Mid(strLicense, 5, 4)
                txtLicense3.Text = Strings.Mid(strLicense, 9, 4)
                txtLicense4.Text = Strings.Mid(strLicense, 13, 4)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click
        Dim sLicenseKey As String
        Dim sNewKey As String

        sNewKey = txtLicense1.Text & txtLicense2.Text & txtLicense3.Text & txtLicense4.Text

        If sNewKey.Length <> 16 Then
            MessageBox.Show("Invalid License Key", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        If Trim(txtLicensee.Text) = "" Then
            MessageBox.Show("Specify licensee information!", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtLicensee.Focus()
            Exit Sub
        End If

        sLicenseKey = Strings.Left(GetLicenseKey(), 16)
        If sLicenseKey <> sNewKey Then
            MessageBox.Show("Invalid License Key", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        SetRegistryValue("LicenseKey", sNewKey)
        SetRegistryValue("Licensee", TEncrypt(Trim(txtLicensee.Text)))

        gbIsLicensed = True
        btnExit_Click(sender, e)
    End Sub
End Class